
<script type="text/javascript" src="<?php echo base_url(); ?>public/js/jquery-ui.js"></script>
<?php
$sidebar = '<ul id=\"competition\" class=\"nav\">';
$last_gender = 'F';
$sidebar.= '<li><a href=\"#\" class=\"disabled\" onclick=\"return false;\">NAISED</a></li>';
foreach ($short_events as $event) {
    if ($event->getGender() != $last_gender) {
        $sidebar.= '<li class=\"divider\"></li>';
        $sidebar.= '<li><a class=\"disabled\" href=\"#\" onclick=\"return false;\">' . strtoupper(Utils::est_gender_string($event->getGender())) . '</a></li>';
    }
    $last_gender = $event->getGender();
    $sidebar.= '<li><a href=\"#' . $event->getNr() . '\">' . Utils::est_event_raw_string($event, true) . '</a></li>';
}
$sidebar.= '</ul>';
$sidebar = str_replace("\n", '\n', $sidebar);
?> 

<style>

    nav.sidebar { 
        left: 0px;
        position: absolute;
    }

    #competition {
        top: 0;
    }

    #competition.fixed {
        position: fixed;
        top: 0;
        bottom: 0;
        overflow: auto;
        margin: 20px 0px 30px 0px;
    }

    #competition.fixed.bottom {
        margin-bottom: 130px;
    }
    #competition::-webkit-scrollbar { 
        display: none; 
    }

</style>


<script>
    
    $(".page-header").html("<h1><?php echo $competition->getName(); ?> </h1>");
    $(".sidebar").html("<?php echo $sidebar; ?>");
    $(document).ready(function () {
        
        $('.result').tooltip({
            items: "[splits]",
            content: function() {
                var element = $( this );
                return element.attr( "splits" );
            }
        });
        
        var top = $('#competition').offset().top - parseFloat($('#competition').css('marginTop').replace(/auto/, 0));
        var height = (document.height !== undefined) ? document.height : document.body.offsetHeight;
        $(window).scroll(function (event) {
            var y = $(this).scrollTop();
            
            if (y >= top) {
                $('#competition').addClass('fixed');
            } else {
                $('#competition').removeClass('fixed');
            }
            if (y >= $(document).height() - $(window).height() - 110) {
                $('#competition').addClass('bottom');
            } else {
                $('#competition').removeClass('bottom');
            }
        });
        
        $('body').attr('data-spy', 'scroll');
        $('body').attr('data-target', '#competition');
        $('body').attr('data-offset', '300');
        $('body').scrollspy();
    });
    
    

</script>

<div class="panel-group" id="accordion1">
    <div class="panel panel-default">
        <div class="panel-heading">
            <a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
                <h4 class="panel-title">Võistluse statistika</h4>
            </a>
        </div>
        <div id="collapseOne" class="panel-collapse collapse">
            <div class="panel-body">
                <h4>
                    <ul class="list-inline">
                        <li>Sportlaseid: <?php echo $num_athletes; ?></li>
                        <li>Klubisid: <?php echo $num_teams; ?></li>
                        <li>Alasid: <?php echo count($events); ?></li>
                        <li>Tulemusi: <?php echo count($results); ?></li>
                    </ul>
                </h4>
                <div id="competition_best_fina">
                    <?php echo $competition_best; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php foreach ($events as /* @var $event Event */ $event) : ?>

    <table class="table table-hover" id="<?php echo $event->getNr(); ?>">
        <thead><tr><th colspan="7">
        <h4>
            <?php echo Utils::est_event_raw_string($event) ?>
        </h4></th></tr>
    <tr>
        <th>Koht</th>
        <th>Nimi</th>
        <th>Sünnia</th>
        <th>Tulemus</th>
        <?php echo $event->isRelay() ? '<th>Võistkond</th>' : '<th>Klubi</th>' ?>
        <th>FINA</th>
    </tr>
    </thead>
    <tbody>
        <?php
        $rank = 1;
        $last_result_ms = 0;
        foreach ($results as /* @var $result Result */ $result) {
            if ($result->getEventID() != $event->getID())
                continue;

            if ($last_result_ms == $result->getResultMS() && $result->getResultMS() != MAX_INT) {
                $rank--;
            }
            if ($rank <= 3 && $result->getResultMS() != MAX_INT && $event->getType() == 'F') {
                echo "<tr class=\"medal-$rank\"><td class=\"medal-$rank\">";
            } else {
                echo '<tr><td class="rank">';
            }
            echo ($result->getResultMS() == MAX_INT || $result->getExhibition() ? '---' : $rank . '.') . '</td>';
            if ($result->getAthleteID() != 0) {
                $athlete = Athlete::fromID($result->getAthleteID());
                echo '<td><a href="' . site_url('athletes/profile/' . $athlete->getID()) . '">' . $athlete->getFirstName() . ' ' . $athlete->getLastName() . '</a></td>';
                echo '<td>' . $athlete->getBirthYear() . '</td>';
                echo '<td class="result"><a href="#" onclick="return false;" splits="' . $result->split_content . '" >' . $result->getResult() . '</a></td>';
                echo '<td>' . $result->getTeam() . '</td>';
            } else if ($result->getRelayID() != 0) {
                $relay = Relay::fromID($result->getRelayID());
                $result->setRelay($relay);
                echo '<td><strong>' . $result->getTeam() . '</strong></td>';
                echo '<td></td><td class="result"><a href="#" onclick="return false;" splits="' . $result->split_content . '" >' . $result->getResult() . '</a></td>';
                echo '<td>' . $relay->getTeam() . '</td>';
            }
            echo '<td>' . $result->getFina() . '</td></tr>';

            if ($result->getRelay() != NULL) {
                $r = $result->getRelay();
                $order = 1;
                foreach (array($r->getAthleteOneID(), $r->getAthleteTwoID(), $r->getAthleteThreeID(), $r->getAthleteFourID()) as $id) {
                    $athlete = Athlete::fromID($id);
                    if ($athlete == NULL) {
                        echo '<td></td><td> ' . $order . ') Info puudub</td></tr>';
                    } else {
                        echo '<td></td><td> ' . $order . ') <a href="' . site_url('athletes/profile/' . $athlete->getID()) . '">' . $athlete->getFirstName() . ' ' . $athlete->getLastName() . '</a></td>';
                        echo '<td>' . $athlete->getBirthYear() . '</td><td colspan="3"></td></tr>';
                    }
                    $order++;
                }
            }
            if ($last_result_ms == $result->getResultMS() && $result->getResultMS() != MAX_INT) {
                $rank++;
            }
            $last_result_ms = $result->getResultMS();
            $rank++;
        }
        echo '</tbody></table>';
    endforeach;
    ?>